﻿using System;
using System.Collections.Generic;
using System.Text;

namespace MatchCore
{
    public class Player
    {
        private Vector2 _shootOffsetPosition;
        private Rect _body;
        private int _speed;
        private uint _index;
        private int _shootDir;
        private int _bloodValue;
        private IPlayerEventer _eventer;

        public Player(uint index,Vector2 pos,Vector2 shootOffsetPos,int speed,int shootDir,int bloodValue)
        {
            this._index = index;
            this._body = new Rect(0,0,Config.PLAYER_SIZE.x, Config.PLAYER_SIZE.y);
            this._body.center = pos;
            this._shootOffsetPosition = shootOffsetPos;
            this._speed = speed;
            this._shootDir = shootDir;
            this._bloodValue = bloodValue;
        }

        public uint GetIndex()
        {
            return _index;
        }

        public Vector2 GetPosition()
        {
            return this._body.center;
        }

        public int GetSpeed()
        {
            return this._speed;
        }

        public void SetEventer(IPlayerEventer eventer)
        {
            _eventer = eventer;
        }

        public int GetShootDir()
        {
            return _shootDir;
        }

        public int GetBlood()
        {
            return _bloodValue;
        }

        public Vector2 GetCurShootPosition()
        {
            return this._body.center + _shootOffsetPosition;
        }

        public void OnShoot(Bullet bullet)
        {
            try
            {
                if (_eventer != null)
                {
                    _eventer.OnShoot(this, bullet);
                }
            }
            catch (Exception ex)
            {
                LogicManager.Instance().LogException("call OnShoot ex:" + ex);
            }
        }

        public bool HitTest(Vector2 point)
        {
            return _body.Contains(point);
        }
           
        public void OnBeShoot(Bullet bullet)
        {
            _bloodValue = _bloodValue - bullet.GetDamageValue();
            
            if(_bloodValue < 0)
            {
                _bloodValue = 0;
            }

            try
            {
                if (_eventer != null)
                {
                    _eventer.OnBeShoot(this, bullet.GetDamageValue());
                }
            }
            catch (Exception ex)
            {
                LogicManager.Instance().LogException("call OnBeShoot ex:" + ex);
            }
        }

        public void OnMove(Vector2 dir)
        {
            Rect body = this._body;
            
            Vector2 center = body.center;

            center.y = center.y + dir.y * _speed;

            body.center = center;

            var sceneRect = LogicManager.Instance().GetSceneRect();

            if(!sceneRect.Contains(body))
            {
                if(body.top < sceneRect.top)
                {
                    body.top = sceneRect.top;
                }
                else if(body.bottom > sceneRect.bottom)
                {
                    body.bottom = sceneRect.bottom;
                }
            }

            this._body = body;

            LogicManager.Instance().LogDebug("player:" + GetIndex() + " (" + body.center.x + "," + body.center.y + ")");
        }

        public void Update(uint frameId)
        {

        }
    }
}
